/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.xy.biz.rs.domain.constants.ManufactureRecordConstants;
import com.xy.tool.context.UserContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.rs.domain.resp.GetManufactureRecordResponse;
import com.xy.biz.rs.domain.req.GetManufactureRecordRequest;
import com.xy.biz.rs.domain.req.ListManufactureRecordRequest;
import com.xy.biz.rs.domain.req.SaveManufactureRecordRequest;
import com.xy.biz.rs.domain.query.ManufactureRecordQuery;
import com.xy.biz.rs.domain.converter.ManufactureRecordConverter;
import com.xy.biz.rs.client.ManufactureRecordClient;
import com.xy.biz.rs.service.ManufactureRecordService;

/**
 *
 *
 * @author wwj
 * @date 2021-4-16
 */
@Api(tags = "生产记录")
@Slf4j
@RestController
@RequestMapping("/api/rs/manufacture/record")
public class ManufactureRecordController implements ManufactureRecordClient {

	@Autowired
	private ManufactureRecordService manufactureRecordService;

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveManufactureRecord(@RequestBody SaveManufactureRecordRequest request) {
		return manufactureRecordService.saveManufactureRecord(ManufactureRecordConverter.toBO(request));
	}

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllManufactureRecord(@RequestBody Collection<SaveManufactureRecordRequest> collection) {
		manufactureRecordService.saveAllManufactureRecord(ManufactureRecordConverter.toBO(collection));
		return Result.success();
	}

	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateManufactureRecord(@RequestBody SaveManufactureRecordRequest request) {
		manufactureRecordService.updateManufactureRecord(ManufactureRecordConverter.toBO(request));
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteManufactureRecord(@RequestBody GetManufactureRecordRequest request) {
		manufactureRecordService.deleteManufactureRecord(request.getManufactureRecordId());
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetManufactureRecordResponse> getManufactureRecord(@RequestBody GetManufactureRecordRequest request) {
		return Result.success(ManufactureRecordConverter.toResponse(manufactureRecordService.getManufactureRecord(request.getManufactureRecordId())));
	}

	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetManufactureRecordResponse>> listManufactureRecordById(@RequestBody Collection<Long> collection) {
		return Result.success(ManufactureRecordConverter.toResponse(manufactureRecordService.listManufactureRecordById(collection)));
	}

	@PostMapping(value = "/list")
	@Override
	public Result<List<GetManufactureRecordResponse>> listManufactureRecord(@RequestBody ListManufactureRecordRequest request) {
		return Result.success(ManufactureRecordConverter.toResponse(manufactureRecordService.listManufactureRecord(ManufactureRecordConverter.toQuery(request))));
	}

	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countManufactureRecord(@RequestBody ListManufactureRecordRequest request) {
		return Result.success(manufactureRecordService.countManufactureRecord(ManufactureRecordConverter.toQuery(request)));
	}


	@ApiOperation(value = "分页查询")
	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetManufactureRecordResponse>> pageManufactureRecord(@RequestBody ListManufactureRecordRequest request) {
		ManufactureRecordQuery query = ManufactureRecordConverter.toQuery(request);
		List<Integer> statusList = new ArrayList<>();
		statusList.add(ManufactureRecordConstants.STATUS_RECORDING);
		query.setStatusList(statusList);
		query.setCreateUserId(UserContextHolder.getUserId());
		List<GetManufactureRecordResponse> list = ManufactureRecordConverter.toResponse(manufactureRecordService.listManufactureRecord(query));
		int count = manufactureRecordService.countManufactureRecord(query);
		return Result.success(new Page<>(list, count));
	}

}
